package com.bbu.javaexam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.domain.Teacher;
import com.bbu.javaexam.service.TeacherService;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName TeacherController.java
 * @Description TODO
 */
@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/teachers/{page}/{size}")
    public RespData findAll(@PathVariable Integer page, @PathVariable Integer size) {
        Page<Teacher> teacherPage = new Page<>(page, size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage);
        return RespDataHandler.buildRespData(200, "查询所有教师", teacherIPage);
    }

    @GetMapping("/teacher/{teacherId}")
    public RespData findById(@PathVariable("teacherId") Integer teacherId) {
        return RespDataHandler.success(teacherService.findById(teacherId));
    }

    @DeleteMapping("/teacher/{teacherId}")
    public RespData deleteById(@PathVariable("teacherId") Integer teacherId) {
        return RespDataHandler.success(teacherService.deleteById(teacherId));
    }

    @PutMapping("/teacher")
    public RespData update(@RequestBody Teacher teacher) {
        return RespDataHandler.success(teacherService.update(teacher));
    }

    @PostMapping("/teacher")
    public RespData add(@RequestBody Teacher teacher) {
        return RespDataHandler.success(teacherService.add(teacher));
    }
}

